<?php

namespace App\Models;

use App\Common\Constant\Params;
use App\Services\IStorageService;
use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $table = 'goods';

    protected $primaryKey = 'go_id';

    const CREATED_AT = 'go_add_time';
    const UPDATED_AT = 'go_update_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'go_id',
        'go_name',
        'go_img',
        'go_me_money',
        'go_me_id',
        'go_status',
        'go_type',
        'go_add_ip',
        'go_model',
        'go_priced_type',
    ];

    protected $casts = [
        'go_me_money' => 'double',
    ];

    public function merchant(){
        return $this->belongsTo(Merchant::class,'go_me_id');
    }

    /**
     * 获取商品图片url绝对地址
     * img_url
     */
    public function getImgUrlAttribute()
    {
        return resolve(IStorageService::class)->getUrl($this->go_img);
    }

    /**
     * 获取商品类目文本
     * type_text
     */
    public function getTypeTextAttribute()
    {
        return Params::$GOODS_CATEGORY[$this->go_type];
    }
}
